package com.blb.service;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.blb.mapper.RoleMapper;
import com.blb.mapper.UserMapper;
import com.blb.mapper.UserroleMapper;
import com.blb.entity.User;
import com.blb.entity.UserExample;
import com.blb.entity.UserExample.Criteria;
import com.blb.entity.Userrole;
import com.blb.entity.UserroleExample;
import com.blb.utils.LayuiPage;
import com.blb.utils.MybatisUtils;
import com.mysql.jdbc.StringUtils;
/**
 * 
 * 业务逻辑层
 * @author Administrator
 *
 */
public class UserService {
	private static final String UserroleExample = null;
	private UserMapper userMapper;
	/**
	 * 添加数据
	 * 
	 * @param user
	 * @throws IOException 
	 */
	public String addUser(User user,String roles) throws IOException {
		SqlSession session = MybatisUtils.getSession();
		userMapper = session.getMapper(UserMapper.class);
		userMapper.insertSelective(user);
		//添加roles
		UserroleMapper userroleMapper = session.getMapper(UserroleMapper.class);
		String[] split=roles.split(",");
		for(String m:split){
			Userrole userrole = new Userrole();
			userrole.setRoleId(m);
			userrole.setUserId(user.getId());
			userroleMapper.insertSelective(userrole);
		}
		session.commit();
		MybatisUtils.closeSession(session);
		return "";
	}
	/**
	 * 
	 * 修改用户
	 * @param id
	 * @return
	 * @throws IOException 
	 */
	public void updateUser(User user,String roles) throws IOException {
		SqlSession session = MybatisUtils.getSession();
		userMapper = session.getMapper(UserMapper.class);
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andIdEqualTo(user.getId());
		userMapper.updateByExampleSelective(user, userExample);
		//把用户角色关联表的信息更新
		 UserroleMapper userroleMapper = session.getMapper(UserroleMapper.class);
		//先删除用户角色关联表中相关信息
		UserroleExample userroleExample = new UserroleExample();
		userroleExample.createCriteria().andUserIdEqualTo(user.getId());
		userroleMapper.deleteByExample(userroleExample);
		//再把勾选的角色信息保存到用户角色关联表中
		String[] roleArr=roles.split(",");
		for(String role:roleArr){
			Userrole userrole = new Userrole();
			userrole.setRoleId(role);
			userrole.setUserId(user.getId());
			userroleMapper.insertSelective(userrole);
		}
		session.commit();
		MybatisUtils.closeSession(session);
	}
	/**
	 * 根据用户ID查询用户
	 * @param id
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
	public User getUserByUserid(String id) throws SQLException, IOException {
		SqlSession session = MybatisUtils.getSession();
		userMapper = session.getMapper(UserMapper.class);

		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();

		criteria.andIdEqualTo(id);

		List<User> selectByExample = userMapper.selectByExample(userExample);

		MybatisUtils.closeSession(session);
		return selectByExample.get(0);
	}
	/**
	 * 根据用户名查询用户
	 * @param userName
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
	public User getUserByUserName(String userName) throws SQLException, IOException {
		SqlSession session = MybatisUtils.getSession();
		userMapper = session.getMapper(UserMapper.class);
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();

		criteria.andUsernameEqualTo(userName);

		User user = null;
		List<User> selectByExample = userMapper.selectByExample(userExample);
		if (selectByExample.size() > 0) {
			user = selectByExample.get(0);
		}

		MybatisUtils.closeSession(session);
		return user;
	}
	/**
	 * 根据用户id删除用户
	 * @param ids
	 * @throws IOException 
	 */
	public String delete(String[] ids) throws IOException {
		SqlSession session = MybatisUtils.getSession();
		userMapper = session.getMapper(UserMapper.class);

		List<String> list = new ArrayList<String>();
		for (String id : ids) {
			list.add(id);
		}

		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andIdIn(list);

		userMapper.deleteByExample(userExample);
		//删除用户角色关联表信息
		
		UserroleMapper userroleMapper = session.getMapper(UserroleMapper.class);
		UserroleExample userroleExample = new UserroleExample();
		userroleExample.createCriteria().andUserIdIn(list);
		userroleMapper.deleteByExample(userroleExample);
		
		session.commit();
		MybatisUtils.closeSession(session);
		return null;
	}
	/**
	 * 用户登录
	 * @throws IOException 
	 * 
	 */
	public List<User> login(String username) throws IOException{
		SqlSession session = MybatisUtils.getSession();
		userMapper = session.getMapper(UserMapper.class);
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<User> list = userMapper.selectByExample(userExample);
		return list;
		
	}

}
